package com.fastloan.mgt.manager.controller;

import com.fastloan.mgt.manager.model.Manager;
import com.fastloan.mgt.manager.model.UserCard;
import com.fastloan.mgt.manager.service.ManagerService;
import com.fastloan.safe.core.util.ManagerKit;
import com.kawins.base.baseabs.BaseController;
import com.kawins.result.ResultEnum;
import com.kawins.result.ResultTo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author chenkai
 *         createTime 2017-11-2017/11/16 14:36
 */
@RestController
@RequestMapping("/api/web/manager")
public class ManagerController extends BaseController<Manager> {

    @Autowired
    private ManagerService managerService;

    /**
     * 获取已初始化的客户经理
     */
    @RequestMapping("/queryAllManager")
    public ResultTo queryManagerTemp() {
        return dataPage("queryManagerAll");
    }

    /**
     * 批量插入客户经理
     * @param managerList
     * @return
     */
    @PutMapping
    public ResultTo insertList(@RequestBody List<Manager> managerList) {
        managerService.insertList(managerList);
        return new ResultTo();
    }

    /**
     * 绑定银行卡
     *
     * @param cardNum 卡号
     * @return 结果
     */
    @PostMapping("/bindCard/{cardNum}/{phone}")
    public ResultTo bindCard(@PathVariable("cardNum") String cardNum,@PathVariable("phone") String phone) {
        return managerService.bindCard(cardNum,phone, ManagerKit.getUserId());
    }

    /**
     * 解绑银行卡
     *
     * @param cardNum 卡号
     * @return 结果
     */
    @DeleteMapping("/unbindCard/{cardNum}")
    public ResultTo unbindCard(@PathVariable("cardNum") String cardNum) {
        int result = managerService
                .deleteCard(new UserCard(ManagerKit.getUserId(),cardNum));
        return result == 1 ? new ResultTo() : new ResultTo(ResultEnum.OPERATION_FAILED);
    }

    /**
     * 获得客户经理银行卡
     *
     * @return 结果
     */
    @GetMapping("/bankCard")
    public ResultTo bankCard() {
        return new ResultTo().setData(managerService.getUserCard(ManagerKit.getUserId()));
    }
}
